package org.teamh.logic.datamodel;

import java.util.concurrent.ConcurrentHashMap;


public class UserAssets {

	private CreepSmashGame game;
	private String ID;
	private int balance;
	private int balanceForSimulation;
	private String color;
	private int income;
	private int lives;
	private User user;
	private boolean visitor;
	private boolean loser;
	private Tile tile;
	private ConcurrentHashMap<String, Tower> towers = new ConcurrentHashMap<String, Tower>();
	private ConcurrentHashMap<String, Creep> creeps = new ConcurrentHashMap<String, Creep>();

	
	public UserAssets(String ID) {
		this.ID = ID;
	}
	
	public CreepSmashGame getGame() {
		return game;
	}
	
	public void setGame(CreepSmashGame game) {
		if(this.game != game) {
			if(getGame() != null) {
				getGame().removeUserAssets(this);
			}
			this.game = game;
			if(getGame() != null) {
				getGame().addUserAssets(this);
			}
		}
	}

	public String getID() {
		return ID;
	}

	public void setID(String iD) {
		ID = iD;
	}

	public int getBalance() {
		return balance;
	}

	public void setBalance(int balance) {
		this.balance = balance;
	}

	public String getColor() {
		return color;
	}

	public void setColor(String color) {
		this.color = color;
	}

	public int getIncome() {
		return income;
	}

	public void setIncome(int income) {
		this.income = income;
	}

	public int getLives() {
		return lives;
	}

	public void setLives(int lives) {
		this.lives = lives;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		if(this.user != user) {
			if(getUser() != null) {
				User oldUser = getUser();
				this.user = null;
				oldUser.setUserAssets(null);
			}
			this.user = user;
			if(getUser() != null) {
				getUser().setUserAssets(this);
			}
		}
	}
	
	public Tile getTile() {
		return tile;
	}
	
	public void setTile(Tile tile) {
		if(this.tile != tile) {
			if(getTile() != null) {
				getTile().setUserAssets(null);
			}
			this.tile = tile;
			if(getTile() != null) {
				getTile().setUserAssets(this);
			}
		}
	}

	public boolean isVisitor() {
		return visitor;
	}

	public void setVisitor(boolean visitor) {
		this.visitor = visitor;
	}
	
	public Tower getTower(String ID) {
		return towers.get(ID);
	}
	
	public void addTower(Tower tower) {
		if(!towers.containsKey(tower.getID())) {
			towers.put(tower.getID(), tower);
			tower.setUserAssets(this);
		}
	}
	
	public void removeTower(Tower tower) {
		if(towers.remove(tower.getID()) != null) {
			tower.setUserAssets(null);
		}
	}
	
	public Creep getCreep(String ID) {
		return creeps.get(ID);
	}
	
	public void addCreep(Creep creep) {
		if(!creeps.containsKey(creep.getID())) {
			creeps.put(creep.getID(), creep);
			creep.setUserAssets(this);
		}
	}
	
	public void removeCreep(Creep creep) {
		if(creeps.remove(creep.getID()) != null) {
			creep.setUserAssets(null);
		}
	}
	
	public ConcurrentHashMap<String, Creep> getCreeps() {
		return creeps;
	}
	
	public ConcurrentHashMap<String, Tower> getTowers() {
		return towers;
	}

	public int getBalanceForSimulation() {
		return balanceForSimulation;
	}

	public void removeFromBalanceForSimulation(int remove) {
		this.balanceForSimulation -= remove;
		//System.out.println("balance for simulation: " + balanceForSimulation);
	}
	
	public void addToBalanceForSimulation(int add) {
		//System.out.println("add to balance: " + add);
		this.balanceForSimulation += add;
		//System.out.println("balance for simulation: " + balanceForSimulation);
	}

	public void setLoser(boolean loser) {
		this.loser = loser;
	}

	public boolean isLoser() {
		return loser;
	}
}
